package org.luxor.cloud.authentication.config;

import org.luxor.cloud.authentication.component.captcha.CaptchaEngine;
import org.luxor.cloud.authentication.component.captcha.CaptchaService;
import org.luxor.cloud.authentication.component.captcha.RedisCaptchaStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.Assert;

/**
 * 验证码配置
 *
 * @author Mr.Yan  @date: 2021/1/8
 */
@Configuration
public class CaptchaConfigurer {

    @Value("${captcha.image-width:250}")
    private int imageWidth;
    @Value("${captcha.image-height:100}")
    private int imageHeight;

    @Value("${captcha.font-size:50}")
    private int fontSize;
    @Value("${captcha.min-word-length:4}")
    private int minWordLength;
    @Value("${captcha.max-word-length:4}")
    private int maxWordLength;

    @Bean
    public CaptchaService captchaService(RedisTemplate<Object, Object> redisTemplate) {
        Assert.notNull(redisTemplate, "Spring容器中找不到[redisTemplate]的实例化对象!");
        CaptchaEngine captchaEngine = new CaptchaEngine(imageWidth, imageHeight, fontSize, minWordLength, maxWordLength);
        RedisCaptchaStore captchaStore = new RedisCaptchaStore(redisTemplate);
        return new CaptchaService(captchaStore, captchaEngine);
    }
}
